<!DOCTYPE html>
<html>

	<head>
		<title>Export map example</title>
		<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="http://openlayers.org/en/3.8.1/css/ol.css" type="text/css">
		<script src="http://openlayers.org/en/3.8.1/build/ol.js"></script>

	</head>

	<body>
		<div class="container-fluid">

			<div class="row-fluid">
				<div class="span12">
					<div id="map" class="map"></div>
					<div id="no-download" class="alert alert-error" style="display: none">
						This example requires a browser that supports the
						<a href="http://caniuse.com/#feat=download">link download</a> attribute.
					</div>
					<a id="export-png" class="btn" download="map.png"><i class="icon-download"></i> Export PNG</a>
				</div>
			</div>

		</div>
		<script>
			var map = new ol.Map({
				layers: [
					new ol.layer.Tile({
						source: new ol.source.OSM()
					}),
					new ol.layer.Vector({
						source: new ol.source.Vector({
							url: 'data/geojson/countries.geojson',
							format: new ol.format.GeoJSON()
						})
					})
				],
				target: 'map',
				controls: ol.control.defaults({
					attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
						collapsible: false
					})
				}),
				view: new ol.View({
					center: [0, 0],
					zoom: 2
				})
			});
			var exportPNGElement = document.getElementById('export-png');
			if ('download' in exportPNGElement) {
				exportPNGElement.addEventListener('click', function(e) {
					map.once('postcompose', function(event) {
						var canvas = event.context.canvas;
						exportPNGElement.href = canvas.toDataURL('image/png');
					});
					map.renderSync();
				}, false);
			} else {
				var info = document.getElementById('no-download');
				/**
				 * display error message
				 */
				info.style.display = '';
			}
		</script>
	</body>

</html>